package com.example.maillist;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;

import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;


import item.Mail;
import service.mailService;

import static android.widget.AdapterView.*;

public class CreateActivity extends AppCompatActivity {

    private Mail mail;
    private mailService mailservice;
    ArrayAdapter<CharSequence> adapter2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_create);
        mailservice = new mailService(this);
        Button btn = findViewById(R.id.goBack);
        Spinner spin = (Spinner) findViewById(R.id.editclass);

        adapter2 = ArrayAdapter.createFromResource(this, R.array.grades, android.R.layout.simple_spinner_item);

        adapter2.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);

        spin.setAdapter(adapter2);
        spin.setVisibility(View.VISIBLE);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(CreateActivity.this,MainActivity.class);
                startActivity(intent);
            }
        });



        Button btn2 =findViewById(R.id.insert);
        btn2.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                mail = new Mail();

                TextView text =findViewById(R.id.editName);
                TextView text2 = findViewById(R.id.editNumber);
                Spinner text3 = findViewById(R.id.editclass);
                TextView text4 = findViewById(R.id.editemail);
                RadioGroup rg = findViewById(R.id.radioGroup);
                final String text5 = ((RadioButton)findViewById(rg.getCheckedRadioButtonId())).getText().toString();

                mail.setName(text.getText().toString());
                mail.setNumber(text2.getText().toString());
                mail.setGrade(text3.getSelectedItem().toString());
                mail.setEmail(text4.getText().toString());
                mail.setGender(text5);

                System.out.println("  "+mail.getName()+" "+mail.getNumber());
                System.out.println(mail);
                mailservice.insert(mail);

                android.content.DialogInterface.OnClickListener listener =
                        new android.content.DialogInterface.OnClickListener(){
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Intent intent = new Intent(CreateActivity.this,MainActivity.class);
                                startActivity(intent);
                            }
                        };
                AlertDialog.Builder builder = new AlertDialog.Builder(CreateActivity.this);
                builder.setTitle("创建成功");
                builder.setPositiveButton("确定",listener);
                builder.show();
            }

        });
    }
    @SuppressLint("WrongConstant")
    public void genderClicked(View view){
        RadioButton button = (RadioButton) view;
        boolean isChecked = button.isChecked();
        switch (view.getId()) {
            case R.id.radio1:
                if (isChecked) {
                    Toast.makeText(CreateActivity.this, button.getText(), 1).show();
                }
                break;
            case R.id.radio2:
                if (isChecked) {
                    Toast.makeText(CreateActivity.this, button.getText(), 1).show();
                }
                break;
            default:
                break;
        }

    }

}